use ggez::{glam::Vec2, Context, graphics};
use crate::particle;




pub struct PraticleSystem {
    particles: Vec<particle::Particle>,
    origin: Vec2,
    lifespan: f32
}

impl PraticleSystem {
    pub fn new(origin: Vec2) -> PraticleSystem {
        let particles: Vec<particle::Particle> = vec![];
        let lifespan = 1.0;
        PraticleSystem { particles, origin, lifespan }
    }

    pub fn is_dead(&self) -> bool {
        if self.particles.len() <= 0 {
            true
        } else {
            false
        }
    }

    pub fn update (&mut self) -> () {
        self.lifespan -= 0.01;
        let location = self.origin;
        if self.lifespan > 0.0 {
            let particle = particle::Particle::new(location);
            self.particles.push(particle);
        }

        self.particles.retain_mut(|particle| {
            let fg = Vec2{x: 0.0, y: 0.1} * particle.get_mass();
            particle.apply_force(fg);
            particle.update();
            !particle.is_dead()
        });
    }

    pub fn display (&mut self, ctx: &mut Context, canvas : &mut graphics::Canvas) -> () {
        for particle in &mut self.particles {particle.display(ctx, canvas)}
    }
}